﻿using Jesai.DynamicDataSource.Sqlsugar.Abstractions;
using Jesai.DynamicDataSource.Sqlsugar.DbConn;
using JESAI.Application.Abstractions.Sys;
using JESAI.Core.Attributes;
using JESAI.Platform.Domain.Sys.Repositories;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace JESAI.Application.Service.Sys
{
    [AppService]
    public class DBConnectionProvider : IDBConnectionProvider
    {
        private readonly IDynamicDataSourceRepository _dynamicDataSourceRepository;
        public DBConnectionProvider(IDynamicDataSourceRepository dynamicDataSourceRepository)
        {
            _dynamicDataSourceRepository = dynamicDataSourceRepository;
        }
        public async Task<List<DBConnection>> GetDbConnections()
        {
            var list = _dynamicDataSourceRepository._DbQueryable.Where(x => x.IsValid == true).ToList();
            List<DBConnection> results = new List<DBConnection>();
            foreach (var item in list)
            {
                DBConnection conn = new DBConnection();
                conn.Name = item.EName;
                conn.ConnectionString = item.ConnectionString;
                conn.DbType = item.DbType;
                results.Add(conn);
            }

            return results;
        }
    }
}
